package com.gsww.vue.service.sys.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gsww.vue.dao.sys.SysAccountDao;
import com.gsww.vue.dao.sys.SysRoleAcctRelDao;
import com.gsww.vue.entity.sys.SysAccount;
import com.gsww.vue.entity.sys.SysRoleAcctRel;
import com.gsww.vue.entity.sys.SysUserSession;
import com.gsww.vue.service.sys.SysLoginService;
import com.gsww.vue.utils.StringHelper;
@Service
@Transactional
public class SysLoginServiceImpl implements SysLoginService {
	@Autowired
	private SysRoleAcctRelDao sysRoleAcctRelDao;
	@Autowired
	private SysAccountDao sysAccountDao;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public SysAccount login(String userName, String password) throws Exception {
		
		List<SysAccount> userList = sysAccountDao.findByLoginAccountAndLoginPassword(userName,password);
		if (userList != null && userList.size() == 1) {
			SysAccount user = userList.get(0);
			/*
			SysUserSession sysUserSession = new SysUserSession();
			sysUserSession.setAccountId(user.getUserAcctId());
			sysUserSession.setUserName(user.getUserName());
			sysUserSession.setDeptId(user.getSysDepartment().getDeptId());
			sysUserSession.setDeptCode(user.getSysDepartment().getDeptCode());
			sysUserSession.setDeptName(user.getSysDepartment().getDeptName());
			sysUserSession.setParentDeptCode(user.getSysDepartment().getParentDeptCode());
			sysUserSession.setParentDeptName(user.getSysDepartment().getParentDeptName());
			sysUserSession.setLoginAccount(user.getLoginAccount());
			List<SysRoleAcctRel> roleList = sysRoleAcctRelDao.findByUserAcctId(user.getUserAcctId());
			String roles = "";
			if(roleList!=null && roleList.size()>0){
				for(SysRoleAcctRel ra : roleList){
					roles += ra.getRoleId().toString()+",";
				}
				roles = roles.substring(0, roles.length()-1);
			}
			sysUserSession.setOperatorList(getOperatorListByRoleIds(roles));
			sysUserSession.setRoleIds(roles);
			sysUserSession.setUserSex(user.getUserSex());
			sysUserSession.setUserState(user.getUserState());
			sysUserSession.setUserTel(user.getUserTele());
			*/
			return user;
		}
		return null;
	}
	@Override
	public SysUserSession getUserInfo(String userId) throws Exception {
		SysUserSession sysUserSession = new SysUserSession();
		if(StringHelper.isNotBlack(userId)){
			SysAccount user = sysAccountDao.findByUserAcctId(userId);
			sysUserSession.setAccountId(user.getUserAcctId());
			sysUserSession.setUserName(user.getUserName());
			sysUserSession.setDeptId(user.getSysDepartment().getDeptId());
			sysUserSession.setDeptCode(user.getSysDepartment().getDeptCode());
			sysUserSession.setDeptName(user.getSysDepartment().getDeptName());
			sysUserSession.setParentDeptCode(user.getSysDepartment().getParentDeptCode());
			sysUserSession.setParentDeptName(user.getSysDepartment().getParentDeptName());
//			sysUserSession.setAreaCode(user.getAreaCode());
			sysUserSession.setLoginAccount(user.getLoginAccount());
			List<SysRoleAcctRel> roleList = sysRoleAcctRelDao.findByUserAcctId(user.getUserAcctId());
//			
			String roles = "";
			if(roleList!=null && roleList.size()>0){
				for(SysRoleAcctRel ra : roleList){
					roles += ra.getRoleId().toString()+",";
				}
				roles = roles.substring(0, roles.length()-1);
			}
			sysUserSession.setOperatorList(getOperatorListByRoleIds(roles));
			sysUserSession.setRoleIds(roles);
			sysUserSession.setUserSex(user.getUserSex());
			sysUserSession.setUserState(user.getUserState());
			sysUserSession.setUserTel(user.getUserTele());
		}
		return sysUserSession;
	}
	public List<String> getOperatorListByRoleIds(String roleIds){
		StringBuilder sb = new StringBuilder("select o.operator_name, o.operator_id \"op_id\", o.operator_image ");
		sb.append(" from sys_operator o ");
		sb.append(" inner join (select ro.operator_id ");
		sb.append("       from sys_role_oper_rel ro");
		sb.append("       where ro.role_id in ("+splitRoleId(roleIds)+") ");
		sb.append("     group by ro.operator_id) roa");
		sb.append("  on o.operator_id = roa.operator_id");
		sb.append("  and o.operator_state = '1' ");
		sb.append("  order by o.operator_level asc ");
		List<Map<String,Object>> list = jdbcTemplate.queryForList(sb.toString());
		List<String> opList = new ArrayList<String>();
		for(Map<String,Object> map : list){
			opList.add(map.get("op_id").toString());
		}
		return opList;
	}
	private String splitRoleId(String roleIds){
		String[] roles = roleIds.split(",");
		String role = "";
		for(int i=0;i<roles.length;i++){
			role += "'"+roles[i]+"',";
		}
		return role.substring(0, role.length()-1);
	}
	
}
